#parse argument

architecture=$1
matrix_decompressed_file=$2
matrix_comp_file=$3
output_dir=$4
lorem_ipsum_file=$5

permutation_str=""

# generate key

str="
xrnlib-cli \
    --encoding-conf -compiled-param -no-password \
    --permutation-conf -arch $architecture $permutation_str \
    --make-monomial-key -monomial-key $output_dir/sym_enc_monomial_key_file.xf 
"
echo ""; echo $str; echo ""
eval $str

# perform encryption

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --encoding-conf -compiled-param -no-password \
    --arithmetic-conf -monomial-key $output_dir/sym_enc_monomial_key_file.xf \
    --sequence-conf -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture \
    --encrypt-symmetric -plain-text $lorem_ipsum_file -cipher-text $output_dir/sym_enc_lorem_ipsum_file "

echo ""; echo $str; echo ""
eval $str

# create a random fractal

str="
xrngen-cli --png -no-prompt -size 2048x2048 -image $output_dir/fractal.png "

echo ""; echo $str; echo ""
eval $str

# split fractal into 3 files

str="
xrnconv-cli --std2xrn -source $output_dir/fractal.png \
                      -metadata $output_dir/fractal.meta \
                      -lsbs $output_dir/fractal.lsbs \
                      -msbs $output_dir/fractal.msbs "

echo ""; echo $str; echo ""
eval $str

# get the capacity of the envelop file

str="
xrnlib-cli \
     --logging-conf -lib-info-log $output_dir/capacity.json \
     --get-capacity -envelop-file $output_dir/fractal.lsbs "

echo ""; echo $str; echo ""
eval $str

# get byte capacity

byte_capacity=$(cat $output_dir/capacity.json | grep byte_capacity | sed 's/.*: //')

echo "# byte_capacity $byte_capacity"

# make steganography block

str="
xrnlib-cli \
    --encoding-conf -compiled-param -no-password \
    --make-steganography-block -offset 0 -tag 0 \
                               -length $(du -b $output_dir/sym_enc_lorem_ipsum_file | cut -f1) \
                               -source-file $output_dir/sym_enc_lorem_ipsum_file \
                               -envelop-file $output_dir/fractal.lsbs \
                               -embedded-file $output_dir/fractal_emb.lsbs "

echo ""; echo $str; echo ""
eval $str

# recreate original image

str="
xrnconv-cli --xrn2std -destination $output_dir/fractal_steg.png \
                      -lsbs $output_dir/fractal_emb.lsbs \
                      -msbs $output_dir/fractal.msbs \
                      -metadata $output_dir/fractal.meta "

echo ""; echo $str; echo ""
eval $str

# extract lsbs from fractal

str="
xrnconv-cli --std2xrn -source $output_dir/fractal_steg.png \
                      -metadata $output_dir/fractal.meta_steg \
                      -lsbs $output_dir/fractal.lsbs_steg \
                      -msbs $output_dir/fractal.msbs_steg "

echo ""; echo $str; echo ""
eval $str

# extract steganography block

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --extract-steganography-block -embedded-file $output_dir/fractal.lsbs_steg -block-file $output_dir/block_file "

echo ""; echo $str; echo ""
eval $str

# remove block header

str="
xrnlib-cli \
    --strip-header-from-block -block-file $output_dir/block_file -plain-text $output_dir/sym_enc_lorem_ipsum_file_stripped "

echo ""; echo $str; echo ""
eval $str

# perform decryption

str="
xrnlib-cli \
    --decoding-conf -compiled-param -no-password \
    --arithmetic-conf -monomial-key $output_dir/sym_enc_monomial_key_file.xf \
    --sequence-conf -xmatrix $matrix_decompressed_file \
    --permutation-conf -arch $architecture \
    --decipher-symmetric -plain-text $output_dir/sym_enc_lorem_ipsum_file_decyphered -cipher-text $output_dir/sym_enc_lorem_ipsum_file_stripped "

echo ""; echo $str; echo ""
eval $str

str=" 
cmp --silent $output_dir/sym_enc_lorem_ipsum_file_decyphered $lorem_ipsum_file || echo 'FAIL'
"
echo ""; echo $str; echo ""
eval $str

